package com.le.ui;

import com.le.component.BackGroundPanel;
import com.le.util.Query;
import com.le.util.ScreenUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MainInterface {
    String hostIpAddress;
    JFrame jFrame = new JFrame("用户登录");
    BackGroundPanel backGroundPanel = new BackGroundPanel(new ImageIcon("images//MainInterface.png").getImage());
    final int WIDTH = 720;
    final int HEIGHT = 480;

    JTextField uName = new JTextField(15);
    JTextField uPassword = new JPasswordField(15);

    Font font = new Font("隶书", Font.PLAIN, 20);

    JLabel logIn = new JLabel("登   录");
    JLabel signUp = new JLabel("注   册");

    JLabel name = new JLabel("用  户  名：");
    JLabel password = new JLabel("密      码：");

    public MainInterface(String hostIpAddress) {
        this.hostIpAddress = hostIpAddress;
    }

    public void init() {
        //组装视图
        jFrame.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2,
                (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);

        jFrame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        jFrame.setResizable(false);
        jFrame.setIconImage(new ImageIcon("images//学生管理-01.png").getImage());

        Box vBox = Box.createVerticalBox();

        Box userNameBox = Box.createHorizontalBox();
        userNameBox.add(Box.createHorizontalStrut(120));
        name.setFont(font);
        userNameBox.add(name);
        userNameBox.add(Box.createHorizontalStrut(10));
        userNameBox.add(uName);
        userNameBox.add(Box.createHorizontalStrut(140));

        Box passwordBox = Box.createHorizontalBox();
        passwordBox.add(Box.createHorizontalStrut(120));
        password.setFont(font);
        passwordBox.add(password);
        passwordBox.add(Box.createHorizontalStrut(10));
        passwordBox.add(uPassword);
        passwordBox.add(Box.createHorizontalStrut(140));

        Box buttonBox = Box.createHorizontalBox();
        logIn.setForeground(Color.black);
        logIn.setFont(new Font("隶书", Font.BOLD, 32));
        signUp.setForeground(Color.black);
        signUp.setFont(new Font("隶书", Font.BOLD, 32));
        buttonBox.add(logIn);
        buttonBox.add(Box.createHorizontalStrut(80));
        buttonBox.add(signUp);

        vBox.add(Box.createVerticalStrut(140));
        vBox.add(userNameBox);
        vBox.add(Box.createVerticalStrut(15));
        vBox.add(passwordBox);
        vBox.add(Box.createVerticalStrut(55));
        vBox.add(buttonBox);
        vBox.add(Box.createVerticalStrut(70));


        backGroundPanel.add(vBox);
        jFrame.add(backGroundPanel);

        jFrame.pack();
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);

        //事件处理
        logIn.addMouseListener(new MyMouseAdapter());
        signUp.addMouseListener(new MyMouseAdapter());
    }

    public class MyMouseAdapter extends MouseAdapter {
        @Override
        public void mouseEntered(MouseEvent e) {
            JLabel login = (JLabel) e.getSource();
            login.setForeground(new Color(65, 105, 225));
            login.setFont(new Font("隶书", Font.BOLD, 40));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            JLabel login = (JLabel) e.getSource();
            login.setForeground(Color.black);
            login.setFont(new Font("隶书", Font.BOLD, 32));
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            JLabel btn = (JLabel) e.getSource();
            String btnText = btn.getText();
            if (btnText.equals("登   录")) {
                try {
                    int logInState = Query.checkCodeBySql(hostIpAddress, uName.getText().trim(), uPassword.getText().trim());
                    switch (logInState) {
                        case 1:
                            JOptionPane.showMessageDialog(jFrame, "登录成功，即将跳转至主页面");
                            new ManageInterface(uName.getText(), hostIpAddress).init();
                            jFrame.dispose();
                            break;
                        case 0:
                            JOptionPane.showMessageDialog(jFrame, "账号不存在！");
                            break;
                        case -1:
                            JOptionPane.showMessageDialog(jFrame, "账号与密码不匹配,请重新再试。");
                    }
                } catch (Exception exception) {
                    JOptionPane.showMessageDialog(jFrame, "连接超时，请检查数据库主机地址!");
                }
            } else {
                JOptionPane.showMessageDialog(jFrame, "即将跳转至注册界面");
                new RegistrationPage(hostIpAddress).init();
                jFrame.dispose();
            }

        }
    }

    public ImageIcon change(ImageIcon image, double i) {
        int width = (int) (image.getIconWidth() * i);
        int height = (int) (image.getIconHeight() * i);
        Image img = image.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
        return new ImageIcon(img);
    }

    public static void main(String[] args) {
        new MainInterface("172.20.10.14").init();
    }
}
